package com.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.bean.wd_ht_project;
import com.dao.wd_ht_projectMapper;
import com.service.ProjectService;
import com.util.Tools;

@Controller
@RequestMapping("/project")
public class ProjectAction {
	@Autowired
	private ProjectService projectservice;
	/**
	 * 有条件查询所有
	 * @param projectname 条件查询的条件
	 * @param model 传参到页面
	 * @return
	 */
	@RequestMapping(value="/selectall.do")
	public String selectAll(String projectname,Model model){
		String pn = null;
		if(projectname!=null&&!"".equals(projectname)){
			pn = Tools.enCodeStr(projectname);//转码
		}
		System.out.println("有条件查询项目名称---------------------------------------"+pn);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("projectName",pn);
		List<wd_ht_project> list = projectservice.selectAll1(map);
		System.out.println(list);
		model.addAttribute("list",list);
		return "ProjectManager";
	}
	/**
	 * 添加项目
	 * @param pbean 添加项目的信息
	 * @return
	 */
	@RequestMapping(value="/insert.do")
	public String insert(wd_ht_project pbean){
		System.out.println("添加项目----------------------------------"+pbean);
		Date day = new Date();
		SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			pbean.setUpdateTime(fm.parse(fm.format(day)));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int num = projectservice.insert(pbean);
		System.out.println(num);
		return "redirect:selectall.do";
	}
	/**
	 * 批量删除(也可删除单个)
	 * @param projectIdArray 要删除的所有项目id,String数组类型
	 * @return
	 */
	@RequestMapping(value="/deletebyprimarykey.do")
	public String deleteByPrimaryKey(String[] projectIdArray){
		for(String s:projectIdArray){
			System.out.println("项目id----------------------------------"+s);
			int num = projectservice.deleteByPrimaryKey(s);
		}
		return "redirect:selectall.do";
	}
	/**
	 * 跳转到添加页面
	 * @return
	 */
	@RequestMapping(value="/add.do")
	public String add(){
		System.out.println("-------------------------------");
		return "ProjectAddManager";
	}
	/**
	 * 跳转到修改页面
	 * @param projectId 要修改的项目id
	 * @param model 传参
	 * @return
	 */
	@RequestMapping(value="/selectone.do")
	public String selectone(String projectId,Model model){
		System.out.println("项目id----------------------------------"+projectId);
		wd_ht_project pbean = projectservice.selectByPrimaryKey(projectId);
		model.addAttribute("pbean",pbean);
		return "ProjectUpdateManager";
	}
	/**
	 * 修改项目信息
	 * @param pbean 修改之后的项目信息
	 * @return
	 */
	@RequestMapping(value="/update.do")
	public String update(wd_ht_project pbean){
		pbean.setUpdateUser("修改人从session中拿！");//写session中修改人的信息
		Date day = new Date();
		SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			pbean.setUpdateTime(fm.parse(fm.format(day)));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		System.out.println("修改后的项目信息----------------------------------"+pbean);
		int num = projectservice.updateByPrimaryKeySelective(pbean);
		return "redirect:selectall.do";
	}
	/**
	 * 处理前台传来的String时间类型,转为Date类型
	 * @param binder 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder) {  
	    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
	    dateFormat.setLenient(false);  
	    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));  
	}
}
